<?php
/**
 * Created by PhpStorm.
 * User: Sixstar-Peter
 * Date: 2018/10/27
 * Time: 21:24
 */


//批量查询key所对应的redis服务器
require  "vendor/autoload.php";

$crc = new \Predis\Cluster\Hash\CRC16();

//槽对应节点信息
$slotNodes = [
    '0, 5460' => '192.168.1.55:7000',
    '5461, 10922' => '192.168.1.55:7001',
    '10923, 16383' => '192.168.1.55:7002'
];

$nodes = ['name11','name22','name33','name44'];

$slotKey = [];
foreach ($nodes as $keyName){
    //1.计算key的哈希值
    $code = $crc->hash($keyName) % 16384;
    //循环匹配范围组合映射关系,(多个key属于一个节点）
    array_walk($slotNodes,function($node,$slotRange) use($code,&$slotKey,$keyName){
        $range = explode(',',$slotRange);
        if($code >= $range[0] && $code <= $range[1] ){
            $slotKey[$node][] = $keyName;
        }
    });
}

var_dump($slotKey);